//
//  SessionTool.swift
//  我的微博
//
//  Created by 吴加将 on 15/9/7.
//  Copyright © 2015年 吴加将. All rights reserved.
//

import UIKit
import AFNetworking
class SessionTool: AFHTTPSessionManager {
    //单列全局的访问点
    static let sharedNetworktool : SessionTool = {
       let tool = SessionTool(baseURL: NSURL(string: "https://api.weibo.com")!)
//        let baseURL = NSURL(string: "https://api.weibo.com/")!
//        let tool = SessionTool(baseURL: baseURL)
//
        tool.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json", "text/json", "text/javascript", "text/plain") as Set<NSObject>
        return tool
    }()
    // 应用程序信息
    private let clientId = "3763573571"
    private let appSecret = "d3e7a54be3676c0d067f252fa5d47c07"
    /// 回调地址
    let redirectUri = "http://www.baidu.com"
    // MARK: -Oatuh 地址
    func oauthUrl() -> NSURL {
        let urlstring = "https://api.weibo.com/oauth2/authorize?client_id=\(clientId)&redirect_uri=\(redirectUri)"
        return NSURL(string: urlstring)!
    }
    func loadToken(code:String,finished:(result:[String:AnyObject]?,error:NSError?)->()){
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id": clientId,
            "client_secret": appSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirectUri]
        POST(urlString, parameters: params, success: { (_, JSON) -> Void in
            finished(result:JSON as? [String:AnyObject],error :nil)
            }, failure: { (_, error) -> Void in
              
            finished(result: nil, error: error)
        })

    }
 
}
